package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.xml.bind.v2.model.core.ID;
import com.usian.article.feign.BeaviorFenign;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleContentMapper;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleService;
import com.usian.model.article.dto.ArticleHomeDto;
import com.usian.model.article.dto.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Auther:ASS
 * @Date:2023/2/22 11:21
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {


    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private BeaviorFenign beaviorFenign;

        @Value("${fileServerUrl}")
        private String fileServerUrl;

    @Override
    public ApArticle saveApArticle(ApArticle apArticle) {

        apArticle.setCreatedTime(new Date());
        this.save(apArticle);
        return apArticle;
    }

    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;

    /**
     * 根据参数加载文章列表
     *
     * @param
     * @param dto
     * @return
     */


    @Override
    public ResponseResult loadList(Short type, ArticleHomeDto dto) {


        //校验参数
        if (dto==null || dto.getSize()==null || dto.getMinBehotTime() ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (StringUtils.isBlank(dto.getTag())){
            dto.setTag("__all__");
        }

        dto.setMinBehotTime(new Date());
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, type);


        ResponseResult responseResult = ResponseResult.okResult(apArticles);
        responseResult.setHost(fileServerUrl);

        return responseResult;
    }

    /**
     * 加载文章详情
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {


        Map<String, Object> resultMap = new HashMap<>();


        //检查参数
        if (dto==null || dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //检查文章的配置
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));

        if (apArticleConfig==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        resultMap.put("config",apArticleConfig);


        //查询文章的内容
        if (!apArticleConfig.getIsDelete() && !apArticleConfig.getIsDown()){
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().
                                eq(ApArticleContent::getArticleId, dto.getArticleId()));

            resultMap.put("content",apArticleContent);
        }

        //结果返回
        return ResponseResult.okResult(resultMap);
    }

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {

        if (dto==null || dto.getArticleId()==null || dto.getAuthorId()==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //要一个当前登录用户 因为fenign要查实体
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //先去查行为实体


        ApBehaviorEntry apBehaviorEntry = beaviorFenign.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());


        Map<String,Object> map=new HashMap<>();
            if (apBehaviorEntry!=null){

                //关注的
                List<ApFollowBehavior> followBehavior = beaviorFenign.findFollowBehavior(apBehaviorEntry.getId(), dto.getArticleId(), dto.getAuthorId());

                    if (followBehavior.size()>0){
                        map.put("isFollow",true);
                    }else {
                        map.put("isFollow",false);
                    }


                    //点赞
                ApLikesBehavior fenignLike = beaviorFenign.findLike(apBehaviorEntry.getId(), dto.getArticleId());
                    if (fenignLike!=null){
                        map.put("isLike",true);
                    }else {
                        map.put("isLike",false);
                    }
            }
        return ResponseResult.okResult(map);
    }
}
